﻿<!DOCTYPE html>
<html>
  <head>
    <!-- meta name="viewport" content="width=device-width, height=device-height, user-scalable=yes, initial-scale=2.0, maximum-scale=4.0, minimum-scale=1.0" / -->
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
    <title>Cordova</title>
    <link rel="stylesheet" href="master.css" type="text/css" media="screen"/>



    <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>


<script type="text/javascript" charset="utf-8">


    var deviceReady = false;

    //-------------------------------------------------------------------------
    // Contacts
    //-------------------------------------------------------------------------
    function getContacts() {
        obj = new ContactFindOptions();
        obj.filter = ""; //Brooks";
        obj.multiple = true;
        navigator.contacts.find(["displayName", "name", "phoneNumbers", "emails", "urls", "note"],
            function(contacts)
			{
				console.log("received contacts :: " + contacts.length);
				console.log("contacts[0] = " + contacts[0]);

                var s = "";
                if (contacts.length == 0)
				{
                    s = "No contacts found";
                }
                else
				{
                    s = "Number of contacts: "+contacts.length+"<br/><table width='100%'><tr><th>Name</th><td>Phone</td><td>Email</td></tr>";

					for (var i=0; i < contacts.length; i++)
					{
						try
						{
                        	var contact = JSON.parse(contacts[i]);
						}
						catch(e)
						{
							console.log(e.message + " " + contacts[i]);
							continue;
						}
                        s = s + "<tr><td>" + contact.displayName + "</td><td>";

                        if (contact.phoneNumbers && contact.phoneNumbers.length > 0)
						{
                            s = s + contact.phoneNumbers[0];
                        }
                        s = s + "</td><td>"

                        if (contact.emails && contact.emails.length > 0)
						{
                            s = s + contact.emails[0];
                        }
                        s = s + "</td></tr>";
                    }
                    s = s + "</table>";
  }

  document.getElementById('contacts_results').innerHTML = s;
  },
  function(e)
  {
  document.getElementById('contacts_results').innerHTML = "Error: "+e.code;
  },
  obj);
  };

  function addContact(){
  console.log("addContact()");
  try{

  console.log("before create");

  // create seems to be failing...
  var contact = navigator.contacts.create({"displayName": "Dooney Evans"});

  console.log("contact created");

  var contactName = {
  formatted: "Dooney Evans",
  familyName: "Evans",
  givenName: "Dooney",
  middleName: ""
  };

  console.log("create contactName");

  contact.name = contactName;

  contact.phoneNumbers = [new ContactField('work', '512-555-1234', true)];

  console.log("create contact phone number");

  contact.save(
  function(id) { console.log("Contact saved." + id);},
  function(e) { console.log("Contact save failed: " + e.code); }
  );

  console.log("saving the contact");
  }
  catch (e)
  {
  //alert(e);
  console.log("failed to save contact");
  }

  };

  /**
  * Function called when page has finished loading.
  */
  function init() {
  document.addEventListener("deviceready", function() {
  deviceReady = true;
  console.log("Device="+device.platform+" "+device.version);
  }, false);
  window.setTimeout(function() {
  if (!deviceReady) {
  alert("Error: Cordova did not initialize.  Demo will not run correctly.");
  }
  },1000);
  }

                    </script>


  </head>
  <body onLoad="init();" id="stage" class="theme">

    <h1>Contacts</h1>
    <div id="info">
        Results:<br/>
        <span id="contacts_results"> </span>
    </div>
    <h2>Action</h2>
    <a href="#" class="btn large" onClick="getContacts();">Get phone's contacts</a>
    <a href="#" class="btn large" onClick="addContact();">Add a new contact 'Dooney Evans'</a>
    <h2> </h2><a href="index.html" class="backBtn">Back</a>
  </body>
</html>